package org.softmed.neodatis.persistence.impl;

import java.util.List;
import org.softmed.persistence.PersistenceManager;
import org.softmed.persistence.PersistenceManagerProvider;
import org.softmed.rest.config.Application;
import org.softmed.rest.server.core.boot.ApplicationProvider;

import com.google.inject.Inject;
import com.google.inject.name.Named;

public class DefaultApplicationProvider implements ApplicationProvider {

	@Inject
	@Named("RESTAdminProviderName")
	static String providerName;

	// Application cfg = null;

	// ODBProviderManager nodb = new ODBProviderManager(providerName);

	@Override
	public List<Application> getApplications() throws Throwable {
		List<Application> apps;

		PersistenceManagerProvider manager = new PersistenceManagerProvider(
				providerName);
		PersistenceManager util = manager.getPersistenceManager();
		util.connect();
		try {
			// try to find a configuration object
			apps = util.getObjects(Application.class);

			// Close ODB
			util.close();

			return apps;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				util.close();
			} catch (Throwable t) {
				t.printStackTrace();
			}

		}

		return null;
	}

	public static String getProviderName() {
		return providerName;
	}

	public static void setProviderName(String providerName) {
		DefaultApplicationProvider.providerName = providerName;
	}

}
